try {
	Type.registerNamespace('TransOutUtil');
	
	TransOutUtil.checkWhenCommit = function(itemKey, scanCount, scanNum) {
		var sql = " where pk_item in ('0'";
		for(var i = 0; i < itemKey.length; i++) {
			sql = sql + ",'" + itemKey[i] + "'";
		}
		sql = sql + ")";
		var checkResult="";
		NCMMPubUtil.queryVO(TransOutScanVO, sql, function(scanData) {
			if(typeof(scanData) == 'undefined' || scanData == null || scanData.length == 0) {
				return;
			}
			var countNum = {};
			var vos = {};
			var data = eval(scanData);
			for(var i = 0; i < data.length; i++) {	
				if(NCMMPubUtil.isEmpty(countNum)||NCMMPubUtil.isEmpty(countNum[data[i].pk_item])) {
					countNum[data[i].pk_item] = 1;
					vos[data[i].pk_item] = data[i];
				} else {
					countNum[data[i].pk_item] = countNum[data[i].pk_item] + 1;
				}
			}
			for(var i = 0; i < itemKey.length; i++) {
				var vo = vos[itemKey[i]];
				if(countNum[itemKey[i]] != scanCount[itemKey[i]]) {
					checkResult= "单据号" + vo.vbillcode + "物料" + vo.cmaterialvid_name + "必须选中其所有扫描记录才能提交！";
				} 
			}
		});
		return checkResult;
	};
	
	TransOutUtil.commitData=function(billinfos,callback){
	    $js.showLoadingBar();	 
	    //测试连接
		var rtn=NCMMDBUtil.testNCConnection(function(data) {
			if(data == false || data == "false") {
				$js.hideLoadingBar();
				$toast("连接服务器失败，无法提交！");
				return false;
			} else {			   
				var tmprtn=NCMMDBUtil.submitLocaldataToNC("4Ysave", billinfos, function(commitInfo) {
					$js.hideLoadingBar();
					if(!NCMMPubUtil.isEmpty(commitInfo)) {
						if(commitInfo.return == null || commitInfo.return == "") {
							$alert("提交失败：" + $jsonToString(commitInfo));
							return false;
						} else if(commitInfo["return"].returnFlag == 1) {												
							var billbids = commitInfo.billbids;
							TransOutUtil.deleteVOsAfterCommit(billbids);
							if(callback != null && callback != undefined && typeof(callback) == 'function') {                                 
                                    callback(billbids);
                                }
                            $toast("提交成功！");								
							return true;						
						} else {
							$alert("提交失败" + $jsonToString(commitInfo.return.returnDesc));
							return false;
						}
					} else {
						$alert("提交失败");
						return false;
					}
				});
				return tmprtn;
			}
		});
		return rtn;
	};
	TransOutUtil.deleteVOsAfterCommit = function(billbids) {
		var sql =""; 
		if(!NCMMPubUtil.isEmpty(billbids)){
			sql=" pk_item in ('0'";
			for(var i = 0; i < billbids.length; i++) {
				sql = sql + ",'" + billbids[i] + "'";
			}
			sql = sql + ")";
		NCMMPubUtil.batchDeleteByCondition(TransOutScanVO, sql);		
		NCMMPubUtil.batchDeleteByCondition(TransOutVO, sql);
		}
	}
	
	TransOutUtil.updateScanedNum= function(pk_item) {
		//更新调拨订单列表增加扫描数量 
		var wheresql = "where pk_item='"+ pk_item+"'";
		NCMMPubUtil.queryVO(TransOutScanVO, wheresql, function(scanData) {
			var num = 0;
			var astnum = 0;
			if(NCMMPubUtil.isNotEmpty(scanData)){
				var scanvos = eval(scanData);		
				for(var i = 0; i < scanvos.length; i++) {
					num = NCMMPubUtil.getAddNum(num, scanvos[i].nscannum);
					astnum = NCMMPubUtil.getAddNum(astnum, scanvos[i].nscanastnum);
				}
			}
			NCMMPubUtil.queryVO(TransOutVO, wheresql, function(data) {
				if(typeof(data) == 'undefined' || data == null || data.length == 0) {
					return;
				}
				var vos = eval(data);
				vos = NCMMPubUtil.htmlspecialjsonarray2UI(vos);
				if(vos == null || vos.length == 0) {
					return;
				}
				//$alert("从数据库读取的数据vo[0]的json格式为：" + $jsonToString(vos[0]));		
				vos[0]["nscanednum"] = num;
				vos[0]["nscanedastnum"] = astnum;
				//3.2 更新调拨订单已扫描主数量
				//$alert("更新数据库的vo[0]的json格式为：" + $jsonToString(vos[0]));
				NCMMPubUtil.updateVO(TransOutVO, vos[0]);
			});
		});
	};
	TransOutUtil.subScanedNum= function(pk_item) {
		//更新调拨订单列表增加扫描数量 
		var wheresql = "where pk_item='"+ pk_item+"'";
		NCMMPubUtil.queryVO(TransOutScanVO, wheresql, function(scanData) {
			if(typeof(scanData) == 'undefined' || scanData == null || scanData.length == 0) {
				return;
			}
			var scanvos = eval(scanData);
			var num = 0;
			var astnum = 0;
			for(var i = 0; i < scanvos.length; i++) {
				num = NCMMPubUtil.getSubNum(num, scanvos[i].nscannum);
				astnum = NCMMPubUtil.getSubNum(astnum, scanvos[i].nscanastnum);
			}
			NCMMPubUtil.queryVO(TransOutVO, wheresql, function(data) {
				if(typeof(data) == 'undefined' || data == null || data.length == 0) {
					return;
				}
				var vos = eval(data);
				vos = NCMMPubUtil.htmlspecialjsonarray2UI(vos);
				if(vos == null || vos.length == 0) {
					return;
				}
				//$alert("从数据库读取的数据vo[0]的json格式为：" + $jsonToString(vos[0]));		
				vos[0]["nscanednum"] = num;
				vos[0]["nscanedastnum"] = astnum;
				//3.2 更新调拨订单已扫描主数量
				//$alert("更新数据库的vo[0]的json格式为：" + $jsonToString(vos[0]));
				NCMMPubUtil.updateVO(TransOutVO, vos[0]);
			});
		});
	};
	 TransOutUtil.updateBillAfterCommit=function(pk_item,num,nastnum){
	   var wheresql = "where pk_item='"+ pk_item+"'";
	 
	   NCMMPubUtil.queryVO(TransOutVO, wheresql,function(data) {
	       var vos = eval(data);
	       vos = NCMMPubUtil.htmlspecialjsonarray2UI(vos);
                if(vos == null || vos.length == 0) {
               
                    return;
                }             
                var newnum= NCMMPubUtil.getSubNum(vos[0]["nscanednum"],num);
                var newnastnum= NCMMPubUtil.getSubNum(vos[0]["nscanedastnum"],nastnum);
                vos[0]["nscanednum"]=newnum;
                vos[0]["nscanedastnum"] = newnastnum;
                vos[0]["nsubmitnum"]= NCMMPubUtil.getAddNum(vos[0]["nsubmitnum"],num);                   
                var ncanscanedNum=NCMMPubUtil.getSubNum(parseFloat(vos[0]["nnum"]),vos[0]["nsubmitnum"]);                
                if(ncanscanedNum<=0){                     
                    //$alert("sql=");           
                    NCMMPubUtil.deleteVO(TransOutVO,vos[0]);
                }
                else{            
                    NCMMPubUtil.updateVO(TransOutVO, vos[0]);
                }	  
	        });
	 }
	 
	TransOutUtil.getScanVOCount = function(pk_item) {
	    var len=0;
		var wheresql = "where pk_item='"+ pk_item+"'";
		NCMMPubUtil.queryVO(TransOutScanVO, wheresql,function(scanData) {
			if(typeof(scanData) == 'undefined' || scanData == null || scanData.length == 0) {
				return;
			}
			var data = eval(scanData);	
			len= data.length;
		});
		return len;
	};
	
	TransOutUtil.openDropDownList = function(controlid) {
		$menu.openDropDownList({
			"controlid": controlid, //目标控件的id
			"dropDownListWidth": "100", //菜单项的宽度
			"background": "#f2f2f2",
			//"background-image":"app.png",
			"color": "#ee4311",
			"font-size": "12",
			"halign": "center", //right  center
			"split-color": "#ee4311",
			//"margin-left":"10",
			"panelstyle": "round-div",
			"border-color": "#ee4311",
			//"showtype":"right",
			"dropItemsArray": [{
				"id": "id_homepage",
				"name": "主菜单", //菜单项名称
				"action": "gotohomepage()" //点击该菜单项时执行的JS方法
			}, {
				"id": "id_datamanage",
				"name": "数据管理", //菜单项名称
				"action": "gotodatamanage()" //点击该菜单项时执行的JS方法
			}, {
				"id": "id_setting",
				"name": "业务设置", //菜单项名称
				"action": "setting()" //点击该菜单项时执行的JS方法
			}, {
				"id": "id_logout",
				"name": "注销", //菜单项名称
				"action": "logout()" //点击该菜单项时执行的JS方法
			}, {
				"id": "id_scanlist",
				"name": "扫描记录", //菜单项名称
				"action": "gotoscanlist()" //点击该菜单项时执行的JS方法
			}]
		});
	};
TransOutUtil.openDropDownListForScan = function(controlid) {
		$menu.openDropDownList({
			"controlid": controlid, //目标控件的id
			"dropDownListWidth": "100", //菜单项的宽度
			"background": "#f2f2f2",
			//"background-image":"app.png",
			"color": "#ee4311",
			"font-size": "12",
			"halign": "center", //right  center
			"split-color": "#ee4311",
			//"margin-left":"10",
			"panelstyle": "round-div",
			"border-color": "#ee4311",
			//"showtype":"right",
			"dropItemsArray": [{
				"id": "id_homepage",
				"name": "主菜单", //菜单项名称
				"action": "gotohomepage()" //点击该菜单项时执行的JS方法
			}, {
				"id": "id_datamanage",
				"name": "数据管理", //菜单项名称
				"action": "gotodatamanage()" //点击该菜单项时执行的JS方法
			}, {
				"id": "id_setting",
				"name": "业务设置", //菜单项名称
				"action": "setting()" //点击该菜单项时执行的JS方法
			}, {
				"id": "id_logout",
				"name": "注销", //菜单项名称
				"action": "logout()" //点击该菜单项时执行的JS方法
			}, {
				"id": "id_scanlist",
				"name": "扫描记录", //菜单项名称
				"action": "gotoscanlistForScan()" //点击该菜单项时执行的JS方法
			}]
		});
	};

	function gotohomepage() {
		$view.open({
			"viewid": "com.yonyou.ncmm_pac_pda.HomePage", //目标页面（首字母大写）全名
			"isKeep": "true", //打开新页面的同时是否保留当前页面，true为保留，false为不保留
		});
	}

	function gotodatamanage() {
		$view.open({
			"viewid": "com.yonyou.ncmm_pac_pda.DM_main", //目标页面（首字母大写）全名
			"isKeep": "true", //打开新页面的同时是否保留当前页面，true为保留，false为不保留
		});
	}

	function setting() {
		$view.open({
			"viewid": "com.yonyou.ncmm_pac_pda.BusinessSettingPage", //目标页面（首字母大写）全名
			"isKeep": "true", //打开新页面的同时是否保留当前页面，true为保留，false为不保留
		});
	}

	function logout() {
		//$view.close();
		$view.open({
			"viewid": "com.yonyou.ncmm_pac_pda.LoginPage", //目标页面（首字母大写）全名
			"isKeep": "false", //打开新页面的同时是否保留当前页面，true为保留， false为不保留
		});
	}

	function gotoscanlist() {
		//$view.close();
		$view.open({
			"viewid": "com.yonyou.ncmm_pac_pda.TransOutEditScanList", //目标页面（首字母大写）全名
			"isKeep": "true", //打开新页面的同时是否保留当前页面，true为保留， false为不保留
		});
	}
	function gotoscanlistForScan() {
		//$view.close();
		$view.open({
			"viewid": "com.yonyou.ncmm_pac_pda.TransOutEditScanList", //目标页面（首字母大写）全名
			"isKeep": "false", //打开新页面的同时是否保留当前页面，true为保留， false为不保留
		});
	}
} catch(e) {
	$e(e);
}